const db = require('../config/database')

// Mock数据
const mockAuthors = [
  {
    id: 1,
    name: '天蚕土豆',
    bio: '知名网络小说作家，代表作《斗破苍穹》',
    avatar_url: 'https://avatars.githubusercontent.com/u/1?v=4'
  },
  {
    id: 2,
    name: '辰东',
    bio: '网络小说大神，以玄幻小说见长',
    avatar_url: 'https://avatars.githubusercontent.com/u/2?v=4'
  },
  {
    id: 3,
    name: '叶天南',
    bio: '都市类小说作家',
    avatar_url: 'https://avatars.githubusercontent.com/u/3?v=4'
  },
  {
    id: 4,
    name: '苏小暖',
    bio: '言情小说作家',
    avatar_url: 'https://avatars.githubusercontent.com/u/4?v=4'
  },
  {
    id: 5,
    name: '机械师',
    bio: '科幻小说作家',
    avatar_url: 'https://avatars.githubusercontent.com/u/5?v=4'
  },
  {
    id: 6,
    name: '历史老王',
    bio: '历史小说作家',
    avatar_url: 'https://avatars.githubusercontent.com/u/6?v=4'
  },
  {
    id: 7,
    name: '唐家三少',
    bio: '知名网络小说作家，代表作《斗罗大陆》',
    avatar_url: 'https://avatars.githubusercontent.com/u/7?v=4'
  }
]

const mockNovels = [
  {
    id: 1,
    title: '斗破苍穹',
    author_id: 1,
    cover_url:
      'https://images.unsplash.com/photo-1481627834876-b7833e8f5570?w=300&h=400&fit=crop',
    description:
      '这里是斗气大陆，没有花俏的魔法，有的，仅仅是繁衍到巅峰的斗气！在这里，斗气就是一切！',
    category: 'fantasy',
    status: 'completed',
    word_count: 5400000,
    chapter_count: 1648,
    rating: 4.6,
    rating_count: 89234,
    view_count: 12500000,
    favorite_count: 345678,
    tags: JSON.stringify(['玄幻', '热血', '升级流', '经典']),
    is_vip: true,
    price_per_chapter: 50000,
    created_at: '2020-03-15 08:00:00',
    updated_at: '2023-08-20 15:30:00'
  },
  {
    id: 2,
    title: '完美世界',
    author_id: 2,
    cover_url:
      'https://images.unsplash.com/photo-1518709268805-4e9042af2176?w=300&h=400&fit=crop',
    description:
      '一粒尘可填海，一根草斩尽日月星辰，弹指间天翻地覆。群雄并起，万族林立，诸圣争霸，乱天动地。',
    category: 'fantasy',
    status: 'ongoing',
    word_count: 6200000,
    chapter_count: 1890,
    rating: 4.8,
    rating_count: 156789,
    view_count: 28900000,
    favorite_count: 567890,
    tags: JSON.stringify(['玄幻', '无敌流', '热血', '经典']),
    is_vip: true,
    price_per_chapter: 50000,
    created_at: '2019-05-10 10:00:00',
    updated_at: '2024-01-15 09:00:00'
  },
  {
    id: 3,
    title: '都市之超级医圣',
    author_id: 3,
    cover_url:
      'https://images.unsplash.com/photo-1576091160399-112ba8d25d1f?w=300&h=400&fit=crop',
    description:
      '一代医圣重生都市，凭借前世的医术和修为，在都市中纵横捭阖，谱写传奇人生。',
    category: 'urban',
    status: 'ongoing',
    word_count: 3200000,
    chapter_count: 980,
    rating: 4.3,
    rating_count: 45678,
    view_count: 8900000,
    favorite_count: 234567,
    tags: JSON.stringify(['都市', '医术', '重生', '爽文']),
    is_vip: false,
    price_per_chapter: 0,
    created_at: '2022-01-20 12:00:00',
    updated_at: '2024-01-14 20:00:00'
  },
  {
    id: 4,
    title: '霸道总裁的小娇妻',
    author_id: 4,
    cover_url:
      'https://images.unsplash.com/photo-1544947950-fa07a98d237f?w=300&h=400&fit=crop',
    description:
      '她是没落千金，他是商界传奇。一场意外的相遇，让两颗心紧紧相依。',
    category: 'romance',
    status: 'completed',
    word_count: 1800000,
    chapter_count: 560,
    rating: 4.1,
    rating_count: 23456,
    view_count: 5600000,
    favorite_count: 123456,
    tags: JSON.stringify(['言情', '霸总', '甜宠', '现代']),
    is_vip: true,
    price_per_chapter: 30000,
    created_at: '2021-06-15 14:00:00',
    updated_at: '2023-12-10 16:30:00'
  },
  {
    id: 5,
    title: '星际争霸之虫族崛起',
    author_id: 5,
    cover_url:
      'https://images.unsplash.com/photo-1446776877081-d282a0f896e2?w=300&h=400&fit=crop',
    description:
      '穿越到星际时代，成为虫族主宰，统领无数虫族，征战星海，称霸宇宙。',
    category: 'scifi',
    status: 'ongoing',
    word_count: 2800000,
    chapter_count: 750,
    rating: 4.5,
    rating_count: 34567,
    view_count: 7800000,
    favorite_count: 189012,
    tags: JSON.stringify(['科幻', '星际', '虫族', '征战']),
    is_vip: true,
    price_per_chapter: 40000,
    created_at: '2022-08-01 16:00:00',
    updated_at: '2024-01-13 18:00:00'
  },
  {
    id: 6,
    title: '大明锦衣卫',
    author_id: 6,
    cover_url:
      'https://images.unsplash.com/photo-1578662996442-48f60103fc96?w=300&h=400&fit=crop',
    description:
      '穿越大明，成为锦衣卫，在朝堂与江湖之间游走，书写不一样的明朝传奇。',
    category: 'historical',
    status: 'ongoing',
    word_count: 4100000,
    chapter_count: 1200,
    rating: 4.4,
    rating_count: 67890,
    view_count: 11200000,
    favorite_count: 298765,
    tags: JSON.stringify(['历史', '穿越', '锦衣卫', '朝堂']),
    is_vip: false,
    price_per_chapter: 0,
    created_at: '2021-03-10 09:00:00',
    updated_at: '2024-01-12 21:00:00'
  },
  {
    id: 7,
    title: '斗罗大陆',
    author_id: 7,
    cover_url:
      'https://images.unsplash.com/photo-1578662996442-48f60103fc96?w=300&h=400&fit=crop',
    description:
      '唐门外门弟子唐三，因偷学内门绝学为唐门所不容，跳崖明志时却发现没有死，反而以另外一个身份来到了另一个世界，一个属于武魂的世界，名叫斗罗大陆。',
    category: 'fantasy',
    status: 'completed',
    word_count: 8888772,
    chapter_count: 336,
    rating: 4.8,
    rating_count: 298765,
    view_count: 45600000,
    favorite_count: 1234567,
    tags: JSON.stringify(['玄幻', '武魂', '穿越', '经典', '唐家三少']),
    is_vip: true,
    price_per_chapter: 50000,
    created_at: '2008-12-14 08:00:00',
    updated_at: '2009-12-13 20:00:00'
  }
]

const mockChapters = [
  // 斗破苍穹的章节
  {
    id: 1,
    novel_id: 1,
    chapter_number: 1,
    title: '陨落的天才',
    content:
      '斗之大陆，没有魔法，没有斗气，有的，仅仅是繁衍到巅峰的武魂！\n\n十级之后，便可凝聚斗之气旋，成为一名真正的斗者！斗之气旋，九旋巅峰，之上便是斗者九星！二星斗者，便可凝聚斗之气旋！\n\n每一旋的提升，实力便是以几何数般暴涨！\n\n而要想成为一名真正强者，必须不断的凝聚斗之气旋，并且还要经历雷劫的考验！',
    word_count: 1580,
    is_vip: false,
    price: 0,
    is_free: true,
    created_at: '2009-04-14 10:00:00',
    updated_at: '2009-04-14 10:00:00'
  },
  {
    id: 2,
    novel_id: 1,
    chapter_number: 2,
    title: '斗之气，三段！',
    content:
      '萧炎，萧家的少族长，曾经的天才少年，如今的废物...\n\n"斗之气，三段！级别：低级！"\n\n测验魔石碑之上，显示着萧炎如今的实力，斗之气三段，这个成绩，即便是在乌坦城之中，也足以算做是天才了！\n\n可是，看着台下众人嘲讽的目光，萧炎的心中却是一片苦涩...',
    word_count: 2140,
    is_vip: true,
    price: 50000,
    is_free: false,
    created_at: '2009-04-15 10:00:00',
    updated_at: '2009-04-15 10:00:00'
  },
  // 斗罗大陆的章节
  {
    id: 3,
    novel_id: 7,
    chapter_number: 0,
    title: '引子',
    content:
      '唐门外门弟子唐三，因偷学内门绝学为唐门所不容，跳崖明志时却发现没有死，反而以另外一个身份来到了另一个世界，一个属于武魂的世界，名叫斗罗大陆。这里没有魔法，没有斗气，没有武术，却有神奇的武魂。',
    word_count: 280,
    is_vip: false,
    price: 0,
    is_free: true,
    created_at: '2008-12-14 10:00:00',
    updated_at: '2008-12-14 10:00:00'
  },
  {
    id: 4,
    novel_id: 7,
    chapter_number: 1,
    title: '斗罗大陆，异界唐三',
    content:
      '圣魂村是诺丁城下属的一个小村子，距离诺丁城二十里。这里民风淳朴，村民们日出而作，日落而息，过着平静的生活。\n\n在村子的最西边，有一间看上去有些破旧的铁匠铺，此时正值正午，烈日当空，但铁匠铺内却传出叮叮当当的打铁声。',
    word_count: 2680,
    is_vip: false,
    price: 0,
    is_free: true,
    created_at: '2008-12-15 10:00:00',
    updated_at: '2008-12-15 10:00:00'
  },
  {
    id: 5,
    novel_id: 7,
    chapter_number: 2,
    title: '废武魂与先天满魂力',
    content:
      '素云涛看着唐三，眼中流露出几分赞赏之色，"很好，你的武魂是蓝银草，虽然是废武魂，但你的魂力却是先天满魂力，这在魂师界是极为罕见的。"',
    word_count: 3240,
    is_vip: true,
    price: 50000,
    is_free: false,
    created_at: '2008-12-16 10:00:00',
    updated_at: '2008-12-16 10:00:00'
  }
]

const mockComments = [
  {
    id: 1,
    novel_id: 1,
    user_id: 2,
    content:
      '经典中的经典！斗破苍穹真的是玄幻小说的巅峰之作，萧炎的成长历程太精彩了！',
    rating: 5,
    like_count: 234,
    reply_count: 12,
    created_at: '2024-01-10 15:30:00',
    updated_at: '2024-01-10 15:30:00'
  },
  {
    id: 2,
    novel_id: 1,
    user_id: 3,
    content:
      '土豆大大的文笔真的没话说，每个角色都刻画得很生动，特别是药老的形象！',
    rating: 5,
    like_count: 156,
    reply_count: 8,
    created_at: '2024-01-09 10:20:00',
    updated_at: '2024-01-09 10:20:00'
  },
  {
    id: 3,
    novel_id: 7,
    user_id: 2,
    content:
      '斗罗大陆永远的神！唐三和小舞的爱情线太感人了，三少大大的世界观设定也很完善！',
    rating: 5,
    like_count: 567,
    reply_count: 23,
    created_at: '2024-01-12 14:20:00',
    updated_at: '2024-01-12 14:20:00'
  }
]

async function importMockData () {
  try {
    console.log('开始导入Mock数据...')

    // 导入作者数据
    console.log('导入作者数据...')
    for (const author of mockAuthors) {
      const existingAuthor = await db.query(
        'SELECT id FROM authors WHERE id = ?',
        [author.id]
      )
      if (existingAuthor.length === 0) {
        await db.query(
          `
          INSERT INTO authors (id, name, bio, avatar_url, created_at, updated_at) 
          VALUES (?, ?, ?, ?, NOW(), NOW())
        `,
          [author.id, author.name, author.bio, author.avatar_url]
        )
        console.log(`导入作者: ${author.name}`)
      }
    }

    // 导入小说数据
    console.log('导入小说数据...')
    for (const novel of mockNovels) {
      const existingNovel = await db.query(
        'SELECT id FROM novels WHERE id = ?',
        [novel.id]
      )
      if (existingNovel.length === 0) {
        await db.query(
          `
          INSERT INTO novels (
            id, title, author_id, cover_url, description, category, status,
            word_count, chapter_count, rating, rating_count, view_count, 
            favorite_count, tags, is_vip, price_per_chapter, created_at, updated_at
          ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        `,
          [
            novel.id,
            novel.title,
            novel.author_id,
            novel.cover_url,
            novel.description,
            novel.category,
            novel.status,
            novel.word_count,
            novel.chapter_count,
            novel.rating,
            novel.rating_count,
            novel.view_count,
            novel.favorite_count,
            novel.tags,
            novel.is_vip,
            novel.price_per_chapter,
            novel.created_at,
            novel.updated_at
          ]
        )
        console.log(`导入小说: ${novel.title}`)
      }
    }

    // 导入章节数据
    console.log('导入章节数据...')
    for (const chapter of mockChapters) {
      const existingChapter = await db.query(
        'SELECT id FROM chapters WHERE id = ?',
        [chapter.id]
      )
      if (existingChapter.length === 0) {
        await db.query(
          `
          INSERT INTO chapters (
            id, novel_id, chapter_number, title, content, word_count,
            is_vip, price, created_at, updated_at
          ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        `,
          [
            chapter.id,
            chapter.novel_id,
            chapter.chapter_number,
            chapter.title,
            chapter.content,
            chapter.word_count,
            chapter.is_vip,
            chapter.price / 100000, // 将50000转换为0.5
            chapter.created_at,
            chapter.updated_at
          ]
        )
        console.log(`导入章节: ${chapter.title}`)
      }
    }

    // 导入评论数据
    console.log('导入评论数据...')
    for (const comment of mockComments) {
      const existingComment = await db.query(
        'SELECT id FROM comments WHERE id = ?',
        [comment.id]
      )
      if (existingComment.length === 0) {
        await db.query(
          `
          INSERT INTO comments (
            id, novel_id, user_id, content, rating, like_count, 
            reply_count, created_at, updated_at
          ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
        `,
          [
            comment.id,
            comment.novel_id,
            comment.user_id,
            comment.content,
            comment.rating,
            comment.like_count,
            comment.reply_count,
            comment.created_at,
            comment.updated_at
          ]
        )
        console.log(`导入评论: ${comment.content.substring(0, 20)}...`)
      }
    }

    console.log('Mock数据导入完成！')
    console.log(
      `共导入: ${mockAuthors.length}个作者, ${mockNovels.length}部小说, ${mockChapters.length}个章节, ${mockComments.length}条评论`
    )
  } catch (error) {
    console.error('导入Mock数据失败:', error)
    throw error
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  importMockData()
    .then(() => {
      console.log('导入完成')
      process.exit(0)
    })
    .catch(error => {
      console.error('导入失败:', error)
      process.exit(1)
    })
}

module.exports = { importMockData }
